/*******************************************************************************
** File Name:		FieldsChooser.js
** Creator:		Howard.L.Chang
** Create date:	2010-10-23
** Latest Modifier:	Howard.L.Chang
** Latest Modify date:	
** Description:		选取Field字段
** Modify History:
**		Modify Date		Modify User		Modify Description
**		 修改日期			  修改人				修改描述
**Code Review:
**		CodeReview Date	CodeReview User	CodeReview Scope
**		CodeReview日期	 CodeReview人		CodeReview的范围
** Version number:   1.0.0
*********************************************************************************/

var FieldsChooser = {
    BaseHtml:
"<table class=\"popup\">\
	<tr>\
		<td class=\"pTL\"></td>\
		<td class=\"pT\"></td>\
		<td class=\"pTR\"></td>\
	</tr>\
	<tr>\
		<td class=\"pML\"></td>\
		<td class=\"pM\">\
			<div class=\"popupTop\"><span class=\"circleL\"></span><span class=\"circleR\"></span>\
				<h4 class=\"popupTitle\"><asp:Literal runat=\"server\" ID=\"literalTitle\"></asp:Literal></h4>\
				<a href=\"javascript:void(0);\" class=\"close\" onClick=\"jQuery.facebox.close(this);\" title=\"Close Popup Window\"></a>\
			</div>\
			<div class=\"popupBody\">\
				<div class=\"areaBody\" style=\"width: 940px;\">\
				    <div id=\"FieldsChoose_Tab\" class=\"tabs\" style=\"display:none;\"></div>\
					<table id=\"FieldsChooseTable\" class=\"grid marginTop\">\
					    <tr><td style=\"height:400px\"></td></tr>\
					</table>\
					<div class=\"pageBar\">\
						<ul class=\"pageNum\" id=\"FieldsChoosePager\">	\
						</ul>\
					</div>\
				</div>\
				<div class=\"areaBtnBar borderTop\">\
					<a href=\"javascript:void(0);\" class=\"buttonC\" onclick=\"jQuery.facebox.close(this)\"><span>取消</span></a>\
					<a href=\"javascript:void(0);\" class=\"buttonB\" onclick=\"FieldsChooser.FinishSelectFields();\"><span>确认</span></a>\
				</div>\
			</div>\
			<div class=\"popupBottom\"><span class=\"circleL\"></span><span class=\"circleR\"></span></div>\
		</td>\
		<td class=\"pMR\"></td>\
	</tr>\
	<tr>\
		<td class=\"pBL\"></td>\
		<td class=\"pB\"></td>\
		<td class=\"pBR\"></td>\
	</tr>\
</table>",
    SelectFields: function(excludeList, selectType, callBack) {
        this.SelectType = selectType;
        if (selectType == "Conditions") {
            this.ThisFieldsCanBeSelected = $.extend(new Array(), ConditionFieldsCanBeSelect);
        }
        else {
            this.ThisFieldsCanBeSelected = $.extend(new Array(), FieldsCanBeSelect);
        }
        this.CallBack = callBack;

        for (var i = this.ThisFieldsCanBeSelected.length - 1; i >= 0; i--) {
            if (selectType == "OrderByFields" && this.ThisFieldsCanBeSelected[i].Type != "BasicInfoField") {
                this.ThisFieldsCanBeSelected.splice(i, 1);
            }
            else if ((selectType == "OrderByFields" && this.ThisFieldsCanBeSelected[i].Remark == "DisplayOnly") ||
                    (selectType == "ShowFields" && this.ThisFieldsCanBeSelected[i].Remark == "OrderOnly")) {
                this.ThisFieldsCanBeSelected.splice(i, 1);
            }
            else if (excludeList != null && excludeList.length != 0) {
                for (var j = 0; j < excludeList.length; j++) {

                    if (this.ThisFieldsCanBeSelected[i].ChineseName == excludeList[j].ChineseName) {
                        if (selectType != "Conditions" || (selectType == "Conditions" &&
                            (excludeList[j].DataType == "Choice" || excludeList[j].DataType == "Boolean" || excludeList[j].DataType == "BooleanNA"))) {
                            this.ThisFieldsCanBeSelected.splice(i, 1);
                            break;
                        }
                    }
                }
            }
        }

        $.each(this.ThisFieldsCanBeSelected, function(i, n) {
            n.Selected = false;
        });

        this.CurrentChooseFiledTablePage = 1;
        jQuery.facebox(this.BaseHtml);
        this.SetTab();
        this.SetFieldsTableHTML();
        this.SetFiledPager();
        $(".facebox").draggable({ handle: '.popupTop', cancel: '.popupBody' });
        FormWidget.dft(".popup");
    },
    CallBack: "",
    SelectType: "",
    ThisFieldsCanBeSelected: new Array(),
    CurrentChooseFiledTablePage: 1,
    SetTab: function() {
        if (this.SelectType == "ShowFields") {
            var TabTitles = new Array();
            TabTitles["BasicInfoField"] = "基本信息";
            TabTitles["Measurement"] = "度量项";
            TabTitles["Target Windage"] = "偏差分析";
            TabTitles["Project Quality Target"] = "项目质量目标";
            TabTitles["Project Team Quality Target"] = "项目组质量目标";
            $("#FieldsChoose_Tab").show();
            for (var i in TabTitles) {
                $("#FieldsChoose_Tab").append("<a href=\"javascript:void(0);\" TabType=\"" + i
                + "\"  onclick=\"FieldsChooser.ChangeFieldsChooseTab(this);\" class=\"tabItem\"><span class=\"circleL\"></span><em>"
                + TabTitles[i] + "</em><span class=\"circleR\"></span></a>");
                $("#FieldsChoose_Tab a:last").data("ItemData", $.grep(this.ThisFieldsCanBeSelected, function(n, j) { return n.Type == i }));
            }
            $("#FieldsChoose_Tab a:first").attr("class", "tabItem current");
        }

        if (this.SelectType == "Conditions") {
            var TabTitles = new Array();
            TabTitles["BasicConditionFields"] = "基本信息";
            TabTitles["Measurement"] = "度量项";
            TabTitles["Quality Target"] = "项目质量目标";
            TabTitles["Project Team Quality Target"] = "项目组质量目标";
            $("#FieldsChoose_Tab").show();
            for (var i in TabTitles) {
                $("#FieldsChoose_Tab").append("<a href=\"javascript:void(0);\" TabType=\"" + i
                + "\"  onclick=\"FieldsChooser.ChangeFieldsChooseTab(this);\" class=\"tabItem\"><span class=\"circleL\"></span><em>"
                + TabTitles[i] + "</em><span class=\"circleR\"></span></a>");
                $("#FieldsChoose_Tab a:last").data("ItemData", $.grep(this.ThisFieldsCanBeSelected, function(n, j) { return n.Type == i }));
            }
            $("#FieldsChoose_Tab a:first").attr("class", "tabItem current");
        }
    },

    SetFieldsTableHTML: function() {
        var ShowItems = this.ThisFieldsCanBeSelected;
        if ($("#FieldsChoose_Tab:visible").length > 0) {
            ShowItems = $("#FieldsChoose_Tab a[class='tabItem current']").data("ItemData");
        }
        var i = 24 * (this.CurrentChooseFiledTablePage - 1);
        var html = "";
        while (i < 24 * this.CurrentChooseFiledTablePage) {
            var j = i % 4;
            if (j == 0) {
                html += "<tr>\n";
            }
            if (i < ShowItems.length) {
                html += String.format("     <td><label><input type=\"checkbox\" FieldName=\"{0}\" />&nbsp;{1}</label></td>\n",
                 ShowItems[i].Name, ShowItems[i].ChineseName);
            }
            else {
                html += "       <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
            }
            if (j == 3) {
                html += "<tr>\n";
            }
            i++;
        }
        $("#FieldsChooseTable").html(html);
        $("#FieldsChooseTable :checkbox").each(function() {
            for (var i = 0; i < FieldsChooser.ThisFieldsCanBeSelected.length; i++) {
                if (FieldsChooser.ThisFieldsCanBeSelected[i].Name == $(this).attr("FieldName")
                && (" " + FieldsChooser.ThisFieldsCanBeSelected[i].ChineseName == $(this).parent().text())) {
                    this.checked = FieldsChooser.ThisFieldsCanBeSelected[i].Selected;
                    break;
                }
            }
        });

        $("#FieldsChooseTable :checkbox").bind("click", function() {
            for (var i = 0; i < FieldsChooser.ThisFieldsCanBeSelected.length; i++) {
                if (FieldsChooser.ThisFieldsCanBeSelected[i].Name == $(this).attr("FieldName")
                && (" " + FieldsChooser.ThisFieldsCanBeSelected[i].ChineseName == $(this).parent().text())) {
                    FieldsChooser.ThisFieldsCanBeSelected[i].Selected = this.checked;
                    break;
                }
            }
        });
        MyFun.grid(".grid tr");
    },
    ChangeFieldsChooseTab: function(obj) {
        $("#FieldsChoose_Tab a").attr("class", "tabItem");
        $(obj).attr("class", "tabItem current");
        this.GotoChooseFieldPage(1);
    },
    SetFiledPager: function() {
        var ShowItems = this.ThisFieldsCanBeSelected;
        if ($("#FieldsChoose_Tab:visible").length > 0) {
            ShowItems = $("#FieldsChoose_Tab a[class='tabItem current']").data("ItemData");
        }
        var FactPageNumber = ((ShowItems.length - 1) / 24 | 0) + 1;
        var html = "							<li class=\"first\"><a href=\"javascript:void(0);\">First</a></li>\n";
        html += "							<li class=\"prev\"><a href=\"javascript:void(0);\">Previous</a></li>\n";



        if (this.CurrentChooseFiledTablePage <= 3 || FactPageNumber <= 5) {
            for (var i = 0; i < FactPageNumber && i < 5; i++) {
                html += "							<li><a href=\"javascript:void(0);\">" + (i + 1) + "</a></li>\n";
            }
        }
        else {
            var startPage = this.CurrentChooseFiledTablePage - 3
            if (FactPageNumber - this.CurrentChooseFiledTablePage < 2) {
                startPage = FactPageNumber - 5;
            }

            for (var i = startPage; i < FactPageNumber && i < parseInt(this.CurrentChooseFiledTablePage) + 2; i++) {
                html += "							<li><a href=\"javascript:void(0);\">" + (i + 1) + "</a></li>\n";
            }
        }

        html += "							<li class=\"next\"><a href=\"javascript:void(0);\">Next</a></li>\n";
        html += "							<li class=\"last\"><a href=\"javascript:void(0);\">Last</a></li>\n";

        $("#FieldsChoosePager").html(html);
        if (this.CurrentChooseFiledTablePage == 1) {
            $("#FieldsChoosePager [class='first'] a").attr("class", "disable");
            $("#FieldsChoosePager [class='prev'] a").attr("class", "disable");
        }
        else {
            $("#FieldsChoosePager [class='first'] a").bind("click", function() { FieldsChooser.GotoChooseFieldPage(1) });
            $("#FieldsChoosePager [class='prev'] a").bind("click", function() { FieldsChooser.GotoChooseFieldPage(FieldsChooser.CurrentChooseFiledTablePage - 1) });
        }

        if (this.CurrentChooseFiledTablePage == FactPageNumber) {
            $("#FieldsChoosePager [class='last'] a").attr("class", "disable");
            $("#FieldsChoosePager [class='next'] a").attr("class", "disable");
        }
        else {
            $("#FieldsChoosePager [class='last'] a").bind("click", function() { FieldsChooser.GotoChooseFieldPage(($("#FieldsChoose_Tab a[class='tabItem current']").data("ItemData").length / 24 | 0) + 1) });
            $("#FieldsChoosePager [class='next'] a").bind("click", function() { FieldsChooser.GotoChooseFieldPage(parseInt(FieldsChooser.CurrentChooseFiledTablePage) + 1) });
        }

        $("#FieldsChoosePager li:gt(1):lt(" + ($("#FieldsChoosePager li").length - 4) + ") a").bind("click", function() { FieldsChooser.GotoChooseFieldPage($(this).text()); });
        $("#FieldsChoosePager li").each(
            function() {
                if ($(this).text() == FieldsChooser.CurrentChooseFiledTablePage) {
                    $(this).attr("class", "current");
                }
            });
    },
    GotoChooseFieldPage: function(toPagerNO) {
        this.CurrentChooseFiledTablePage = toPagerNO;
        this.SetFieldsTableHTML();
        this.SetFiledPager();
        MyFun.grid(".grid tr");
    },

    FinishSelectFields: function() {
        var SelectedFields = new Array();
        var i = 0;
        $.each(FieldsChooser.ThisFieldsCanBeSelected, function(j, n) {
            if (n.Selected) {
                SelectedFields[i++] = n;
            }
        });
        jQuery.facebox.close(document.getElementById("FieldsChooseTable"));
        eval(FieldsChooser.CallBack + "(SelectedFields)");

    }
}